<%@ page import="com.huan.service.NewsServiceImpl" %>
<%@ page import="com.huan.service.NewsService" %>
<%@ page import="com.huan.entity.News" %>
<%@ page import="java.util.List" %>
<%@ page import="com.huan.util.PageHelper" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>无标题文档</title>
    <link type="text/css" rel="stylesheet" href="<%=request.getContextPath() %>/css/common.css"/>
    <style type="text/css">
    </style>
</head>

<body>
<div class="main-content-right">
    <!--即时新闻-->
    <div class="main-text-box">
        <div class="main-text-box-tbg">
            <div class="main-text-box-bbg">
                <form name="searchForm" id="searchForm" action="newsDetailList.jsp" method="post">
                    <div>
                        新闻分类：
                        <select name="categoryId">
                            <option value="0">全部</option>

                            <option value='1'>国内</option>

                            <option value='2'>国际</option>

                            <option value='3'>娱乐</option>

                            <option value='4'>军事</option>

                            <option value='5'>财经</option>

                            <option value='6'>天气</option>

                        </select>
                        新闻标题<input type="text" name="title" id="title" value=''/>
                        <button type="submit" class="page-btn">GO</button>
                        <button type="button" onclick="addNews();" class="page-btn">增加</button>
                        <input type="hidden" name="currentPageNo" value="1"/>
                        <input type="hidden" name="pageSize" value="10"/>
                        <input type="hidden" name="totalPageCount" value="2"/>
                    </div>
                </form>
                <table cellpadding="1" cellspacing="1" class="admin-list">
                    <thead>
                    <tr class="admin-list-head">
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        request.setCharacterEncoding("UTF-8");
                        response.setCharacterEncoding("UTF-8");

                        String categoryId = request.getParameter("categoryId");
                        int categoryID = categoryId != null && !"".equals(categoryId) ? Integer.valueOf(categoryId) : 0;
                        String title = request.getParameter("title");


                            NewsService newsDetailService = new NewsServiceImpl();
						//查询新闻总数
                        Integer total = newsDetailService.getRecordCount(categoryID, title);
                        PageHelper pageHelper = new PageHelper();
                        String currentPageNo = request.getParameter("currentPageNo");
						pageHelper.setRecordCount(total);
						pageHelper.setTotalPageCount();
                        if (currentPageNo == null || "".equals(currentPageNo)) {
                            pageHelper.setCurrentPageNo(1);
                        } else {
                            Integer currentPage = Integer.valueOf(currentPageNo);
							//异常  currentPage<1 || currentPage > totalPageCount
							if(currentPage<1){
								currentPage=1;
							}
							if(currentPage>pageHelper.getTotalPageCount()){
								currentPage=pageHelper.getTotalPageCount();
							}
                            pageHelper.setCurrentPageNo(currentPage);
                        }
						//条件分页查询
                        List<News> newsDetailList = newsDetailService.getNewListWithPaingInfo(categoryID, title,
								pageHelper.getCurrentPageNo(),pageHelper.getPageSize());
                        pageContext.setAttribute("news",newsDetailList);
                    %>
                    <c:if test="${news!=null && news.size()>0}">
                        <c:forEach items="${news}" var="newsDetail" varStatus="varStatus">
                            <tr <c:if test="${varStatus.count % 2 ==0}"> class="admin-list-td-h2"</c:if>>
                                <td><a href='adminNewsView.jsp?id=${newsDetail.id}'>
                                        <%--                            <%=newsDetail.getTitle() %>--%>
                                    <c:out value="${newsDetail.title}" default="无标签" escapeXml="true"/>
                                </a></td>
                                <td>${newsDetail.author}
                                </td>
                                <td>${newsDetail.createdate}
                                </td>
                                <td><a href="/pages/admin/newsDetailCreateSimple.jsp?id=${newsDetail.id}">修改</a>
                                    <a href="javascript:if(confirm('确认是否删除此新闻？')) window.location.href='/servlet/doDel?deleteId=${newsDetail.id}'">删除</a>
                                    <script type="text/javascript">
                                    </script>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:if>

<%--                   <% } %>--%>
                    <%--  }--%>
                    </tbody>
                </table>
                <div class="page-bar">
                    <%
                        int count = newsDetailService.getRecordCount(categoryID, title);
                    %>
                    <ul class="page-num-ul clearfix">
                        <li>共<%=count%>条记录&nbsp;&nbsp;&nbsp; <%=pageHelper.getCurrentPageNo()%>/<%=pageHelper.getTotalPageCount()%>页</li>
                        &nbsp;&nbsp;&nbsp;
                        <%
                        if(pageHelper.getCurrentPageNo()>1){
                        %>
                        <a href="/pages/admin/newsDetailList.jsp?currentPageNo=1">首页</a>
                        &nbsp;&nbsp;
                        <a href="/pages/admin/newsDetailList.jsp?currentPageNo=<%=pageHelper.getCurrentPageNo() -1 %>">上一页</a>
                        <%}%>
                        &nbsp;&nbsp;
                        <%
                        if(pageHelper.getCurrentPageNo()!=pageHelper.getTotalPageCount()){
                        %>
                        <a href="/pages/admin/newsDetailList.jsp?currentPageNo=<%=pageHelper.getCurrentPageNo() + 1 %>">下一页</a>
                        &nbsp;&nbsp;&nbsp;
                        <a href="/pages/admin/newsDetailList.jsp?currentPageNo=<%=pageHelper.getTotalPageCount()%>">最后一页</a>&nbsp;&nbsp;
                        <%}%>
                    </ul>
                    <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key"/>页
	     <button type="button" class="page-btn"
                 onclick='jump_to(document.getElementById("inputPage").value)'>GO</button>
		</span>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function addNews() {
        window.location.href = "newsDetailCreateSimple.jsp";
    }

    function jump_to(pageNo) {
        window.location.href="/pages/admin/newsDetailList.jsp?currentPageNo="+pageNo;
    }
</script>


</body>
</html>
